﻿using SHomeWorkshop.LunarMind.LMETools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SHomeWorkshop.LunarMind.Dialogs
{
    /// <summary>
    /// GraphicDemonstrator.xaml 的交互逻辑
    /// </summary>
    public partial class GraphicDemonstrator : Window
    {
        public GraphicDemonstrator(List<DemoResourceGroup> groups)
        {
            InitializeComponent();

            this.ResourceGroups = groups;
            foreach (var group in groups)
            {
                group.GraphicDemonstrator = this;
            }
        }

        public Canvas MainCanvas { get { return mainCanvas; } }

        public List<DemoResourceGroup> ResourceGroups;

        private void btnSwitch_Click(object sender, RoutedEventArgs e)
        {
            if (Grid.GetColumn(gdTools) == 0)  // 如果当前在左侧
            {
                // 返回右侧
                Grid.SetColumn(gdTools, 2);
                Grid.SetColumn(btnSwitch, 0);
                Grid.SetColumn(ugResources, 1);
                cdSwitch.Width = new GridLength(1, GridUnitType.Auto);
                cdResources.Width = new GridLength(1, GridUnitType.Star);
            }
            else
            {
                // 在右侧，切换到左侧
                Grid.SetColumn(gdTools, 0);
                Grid.SetColumn(btnSwitch, 1);
                Grid.SetColumn(ugResources, 0);
                cdSwitch.Width = new GridLength(1, GridUnitType.Star);
                cdResources.Width = new GridLength(1, GridUnitType.Auto);
            }

            this.InvalidateVisual();
            this.UpdateLayout();

            btn_layout_default_Click(sender, e);
        }

        private void btn_layout_default_Click(object sender, RoutedEventArgs e)
        {
            var cards = OpenedCards;

            if (cards.Count <= 0) return;

            var hspan = 40;
            var vspan = 40;

            var leftTop = this.mainCanvas.PointToScreen(new Point(20, 20));
            var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

            for (int i = 0; i < cards.Count; i++)
            {
                cards[i].Left = leftTop.X + hspan * i;
                cards[i].Top = leftTop.Y + vspan * i;

                cards[i].Width = this.mainCanvas.ActualWidth - hspan * cards.Count;
                cards[i].Height = this.mainCanvas.ActualHeight - vspan * cards.Count;

                cards[i].Activate();
            }
        }

        public DemoResourceCard ActivedCard { get; set; } = null;

        private void btn_layout_maxsize_Click(object sender, RoutedEventArgs e)
        {
            var activedCard = ActivedCard;
            if (activedCard != null)
            {
                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                //var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                activedCard.Width = mainCanvas.ActualWidth;
                activedCard.Height = mainCanvas.ActualHeight;

                activedCard.Left = leftTop.X;
                activedCard.Top = leftTop.Y;
            }
            else
            {
                MessageBox.Show("请先选中一个资源演示窗口！", Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
        }

        private void btn_layout_minsize_Click(object sender, RoutedEventArgs e)
        {
            var activedCard = ActivedCard;
            if (activedCard != null)
            {
                activedCard.Close();
                activedCard.MasterResourceButton.IsChecked = false;

                ActivedCard = null;
            }
            else
            {
                MessageBox.Show("请先选中一个资源演示窗口！", Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
        }

        private List<DemoResourceCard> OpenedCards
        {
            get
            {
                var cards = new List<DemoResourceCard>();
                foreach (var element in this.ugResources.Children)
                {
                    if (element is DemoResourceButton dbtn)
                    {
                        if (dbtn.ResourceCard.Visibility == Visibility.Visible)
                        {
                            cards.Add(dbtn.ResourceCard);
                        }
                    }
                }
                return cards;
            }
        }

        private void btn_layout_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var tag = (sender as Button).Tag as string;
                switch (tag)
                {
                    case "2-h":    // 横向2个
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 2)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 2;
                                cards[0].Height = mainCanvas.ActualHeight;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 2;
                                cards[1].Height = mainCanvas.ActualHeight;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;
                            }
                            break;
                        }
                    case "2-v":    //  纵向2个
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 2)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth;
                                cards[0].Height = mainCanvas.ActualHeight / 2;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth;
                                cards[1].Height = mainCanvas.ActualHeight / 2;
                                cards[1].Left = leftTop.X;
                                cards[1].Top = leftTop.Y + cards[0].Height;
                            }
                            break;
                        }
                    case "3-l":   // 左1右2
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 3)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 2;
                                cards[0].Height = mainCanvas.ActualHeight;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 2;
                                cards[1].Height = mainCanvas.ActualHeight / 2;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;

                                cards[2].Width = mainCanvas.ActualWidth / 2;
                                cards[2].Height = mainCanvas.ActualHeight / 2;
                                cards[2].Left = leftTop.X + cards[1].Width;
                                cards[2].Top = leftTop.Y + cards[1].Height;
                            }
                            break;
                        }
                    case "3-t":   // 上1下2
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 3)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth;
                                cards[0].Height = mainCanvas.ActualHeight / 2;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 2;
                                cards[1].Height = mainCanvas.ActualHeight / 2;
                                cards[1].Left = leftTop.X;
                                cards[1].Top = leftTop.Y + cards[0].Height;

                                cards[2].Width = mainCanvas.ActualWidth / 2;
                                cards[2].Height = mainCanvas.ActualHeight / 2;
                                cards[2].Left = leftTop.X + cards[1].Width;
                                cards[2].Top = leftTop.Y + cards[1].Height;
                            }
                            break;
                        }
                    case "3-r":   // 右1左2
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 3)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 2;
                                cards[0].Height = mainCanvas.ActualHeight / 2;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 2;
                                cards[1].Height = mainCanvas.ActualHeight / 2;
                                cards[1].Left = leftTop.X;
                                cards[1].Top = leftTop.Y + cards[0].Height;

                                cards[2].Width = mainCanvas.ActualWidth / 2;
                                cards[2].Height = mainCanvas.ActualHeight;
                                cards[2].Left = leftTop.X + cards[0].Width;
                                cards[2].Top = leftTop.Y;
                            }
                            break;
                        }
                    case "3-b":   //  下1上2
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 3)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 2;
                                cards[0].Height = mainCanvas.ActualHeight / 2;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 2;
                                cards[1].Height = mainCanvas.ActualHeight / 2;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;

                                cards[2].Width = mainCanvas.ActualWidth;
                                cards[2].Height = mainCanvas.ActualHeight / 2;
                                cards[2].Left = leftTop.X;
                                cards[2].Top = leftTop.Y + cards[0].Height;
                            }
                            break;
                        }
                    case "3-a":   // 三个横排
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 3)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 3;
                                cards[0].Height = mainCanvas.ActualHeight;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 3;
                                cards[1].Height = mainCanvas.ActualHeight;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;

                                cards[2].Width = mainCanvas.ActualWidth / 3;
                                cards[2].Height = mainCanvas.ActualHeight;
                                cards[2].Left = leftTop.X + cards[0].Width * 2;
                                cards[2].Top = leftTop.Y;
                            }
                            break;
                        }
                    case "4":     // 田字布局
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 4)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 2;
                                cards[0].Height = mainCanvas.ActualHeight / 2;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 2;
                                cards[1].Height = mainCanvas.ActualHeight / 2;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;

                                cards[2].Width = mainCanvas.ActualWidth / 2;
                                cards[2].Height = mainCanvas.ActualHeight / 2;
                                cards[2].Left = leftTop.X;
                                cards[2].Top = leftTop.Y + cards[0].Height;

                                cards[3].Width = mainCanvas.ActualWidth / 2;
                                cards[3].Height = mainCanvas.ActualHeight / 2;
                                cards[3].Left = leftTop.X + cards[0].Width;
                                cards[3].Top = leftTop.Y + cards[0].Height;
                            }
                            break;
                        }
                    case "5-l":   // 左1右4（4呈田字布局）
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 5)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 3;
                                cards[0].Height = mainCanvas.ActualHeight;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 3;
                                cards[1].Height = mainCanvas.ActualHeight / 2;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;

                                cards[2].Width = mainCanvas.ActualWidth / 3;
                                cards[2].Height = mainCanvas.ActualHeight / 2;
                                cards[2].Left = leftTop.X + cards[0].Width * 2;
                                cards[2].Top = leftTop.Y;

                                cards[3].Width = mainCanvas.ActualWidth / 3;
                                cards[3].Height = mainCanvas.ActualHeight / 2;
                                cards[3].Left = leftTop.X + cards[0].Width;
                                cards[3].Top = leftTop.Y + cards[2].Height;

                                cards[4].Width = mainCanvas.ActualWidth / 3;
                                cards[4].Height = mainCanvas.ActualHeight / 2;
                                cards[4].Left = leftTop.X + cards[0].Width * 2;
                                cards[4].Top = leftTop.Y + cards[2].Height;
                            }
                            break;
                        }
                    case "5-t":   // 上1下4（4呈田字布局）
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 5)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth;
                                cards[0].Height = mainCanvas.ActualHeight / 3;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 2;
                                cards[1].Height = mainCanvas.ActualHeight / 3;
                                cards[1].Left = leftTop.X;
                                cards[1].Top = leftTop.Y + cards[0].Height;

                                cards[2].Width = mainCanvas.ActualWidth / 2;
                                cards[2].Height = mainCanvas.ActualHeight / 3;
                                cards[2].Left = leftTop.X + cards[1].Width;
                                cards[2].Top = leftTop.Y + cards[0].Height;

                                cards[3].Width = mainCanvas.ActualWidth / 2;
                                cards[3].Height = mainCanvas.ActualHeight / 3;
                                cards[3].Left = leftTop.X;
                                cards[3].Top = leftTop.Y + cards[0].Height * 2;

                                cards[4].Width = mainCanvas.ActualWidth / 2;
                                cards[4].Height = mainCanvas.ActualHeight / 3;
                                cards[4].Left = leftTop.X + cards[3].Width;
                                cards[4].Top = leftTop.Y + cards[0].Height * 2;
                            }
                            break;
                        }
                    case "5-r":   // 右1左4（4呈田字布局）
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 5)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 3;
                                cards[0].Height = mainCanvas.ActualHeight / 2;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 3;
                                cards[1].Height = mainCanvas.ActualHeight / 2;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;

                                cards[2].Width = mainCanvas.ActualWidth / 3;
                                cards[2].Height = mainCanvas.ActualHeight / 2;
                                cards[2].Left = leftTop.X;
                                cards[2].Top = leftTop.Y + cards[0].Height;

                                cards[3].Width = mainCanvas.ActualWidth / 3;
                                cards[3].Height = mainCanvas.ActualHeight / 2;
                                cards[3].Left = leftTop.X + cards[0].Width;
                                cards[3].Top = leftTop.Y + cards[0].Height;

                                cards[4].Width = mainCanvas.ActualWidth / 3;
                                cards[4].Height = mainCanvas.ActualHeight;
                                cards[4].Left = leftTop.X + cards[0].Width * 2;
                                cards[4].Top = leftTop.Y;
                            }
                            break;
                        }
                    case "5-b":   // 下1上4（4呈田字布局）
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 5)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 2;
                                cards[0].Height = mainCanvas.ActualHeight / 3;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 2;
                                cards[1].Height = mainCanvas.ActualHeight / 3;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;

                                cards[2].Width = mainCanvas.ActualWidth / 2;
                                cards[2].Height = mainCanvas.ActualHeight / 3;
                                cards[2].Left = leftTop.X;
                                cards[2].Top = leftTop.Y + cards[0].Height;

                                cards[3].Width = mainCanvas.ActualWidth / 2;
                                cards[3].Height = mainCanvas.ActualHeight / 3;
                                cards[3].Left = leftTop.X + cards[0].Width;
                                cards[3].Top = leftTop.Y + cards[0].Height;

                                cards[4].Width = mainCanvas.ActualWidth;
                                cards[4].Height = mainCanvas.ActualHeight / 3;
                                cards[4].Left = leftTop.X;
                                cards[4].Top = leftTop.Y + cards[0].Height * 2;
                            }
                            break;
                        }
                    case "6-h":
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 6)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 2;
                                cards[0].Height = mainCanvas.ActualHeight / 3;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 2;
                                cards[1].Height = mainCanvas.ActualHeight / 3;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;

                                cards[2].Width = mainCanvas.ActualWidth / 2;
                                cards[2].Height = mainCanvas.ActualHeight / 3;
                                cards[2].Left = leftTop.X;
                                cards[2].Top = leftTop.Y + cards[0].Height;

                                cards[3].Width = mainCanvas.ActualWidth / 2;
                                cards[3].Height = mainCanvas.ActualHeight / 3;
                                cards[3].Left = leftTop.X + cards[0].Width;
                                cards[3].Top = leftTop.Y + cards[0].Height;

                                cards[4].Width = mainCanvas.ActualWidth / 2;
                                cards[4].Height = mainCanvas.ActualHeight / 3;
                                cards[4].Left = leftTop.X;
                                cards[4].Top = leftTop.Y + cards[0].Height * 2;

                                cards[5].Width = mainCanvas.ActualWidth / 2;
                                cards[5].Height = mainCanvas.ActualHeight / 3;
                                cards[5].Left = leftTop.X + cards[0].Width;
                                cards[5].Top = leftTop.Y + cards[0].Height * 2;
                            }
                            break;
                        }
                    case "6-v":
                        {
                            var cards = OpenedCards;
                            if (cards.Count == 6)
                            {
                                var leftTop = this.mainCanvas.PointToScreen(new Point(0, 0));
                                var rightBottom = this.mainCanvas.PointToScreen(new Point(this.mainCanvas.ActualWidth, this.mainCanvas.ActualHeight));

                                cards[0].Width = mainCanvas.ActualWidth / 3;
                                cards[0].Height = mainCanvas.ActualHeight / 2;
                                cards[0].Left = leftTop.X;
                                cards[0].Top = leftTop.Y;

                                cards[1].Width = mainCanvas.ActualWidth / 3;
                                cards[1].Height = mainCanvas.ActualHeight / 2;
                                cards[1].Left = leftTop.X + cards[0].Width;
                                cards[1].Top = leftTop.Y;

                                cards[2].Width = mainCanvas.ActualWidth / 3;
                                cards[2].Height = mainCanvas.ActualHeight / 2;
                                cards[2].Left = leftTop.X + cards[0].Width * 2;
                                cards[2].Top = leftTop.Y;

                                cards[3].Width = mainCanvas.ActualWidth / 3;
                                cards[3].Height = mainCanvas.ActualHeight / 2;
                                cards[3].Left = leftTop.X;
                                cards[3].Top = leftTop.Y + cards[0].Height;

                                cards[4].Width = mainCanvas.ActualWidth / 3;
                                cards[4].Height = mainCanvas.ActualHeight / 2;
                                cards[4].Left = leftTop.X + cards[0].Width;
                                cards[4].Top = leftTop.Y + cards[0].Height;

                                cards[5].Width = mainCanvas.ActualWidth / 3;
                                cards[5].Height = mainCanvas.ActualHeight / 2;
                                cards[5].Left = leftTop.X + cards[0].Width * 2;
                                cards[5].Top = leftTop.Y + cards[0].Height;
                            }
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        private void btnPreviewGroup_Click(object sender, RoutedEventArgs e)
        {
            this.ShowGroup(--groupIndex);
        }

        private void btnNextGroup_Click(object sender, RoutedEventArgs e)
        {
            this.ShowGroup(++groupIndex);
        }

        private int groupIndex = 0;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.ResourceGroups.Count > 0)
            {
                this.ShowGroup(0);
            }
        }

        public UniformGrid UgResources { get { return ugResources; } }

        private void ShowGroup(int index)
        {
            if (index < 0 || this.ResourceGroups.Count <= 0 || index >= this.ResourceGroups.Count) return;

            ugResources.Children.Clear();

            this.mainCanvas.Children.Clear();

            this.groupIndex = index;

            var group = this.ResourceGroups[this.groupIndex];

            if (group.Count <= 0)
            {
                MessageBox.Show("此分组中无资源！", Globals.AppName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            DemoResourceButton fstbtn = null;
            foreach (var ri in group.Resources)
            {
                var dbtn = new DemoResourceButton(this, ri, group.GraphicDemonstrator.MainCanvas);
                ugResources.Children.Add(dbtn);
                if (fstbtn == null) fstbtn = dbtn;
            }

            tbGroupTitle.Text = group.Title;

            tbGroupIndex.Text = $"{index + 1} / {this.ResourceGroups.Count}";

            fstbtn?.ShowCard();
        }

        private void mainCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            foreach (var element in mainCanvas.Children)
            {
                if (element is DemoResourceCard card)
                {
                    card.Height = mainCanvas.ActualHeight / 2;
                    card.Width = mainCanvas.ActualWidth / 2;
                }
            }
        }
    }
}
